package com.chaopei.znpk.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.chaopei.znpk.entity.Course;
import com.chaopei.znpk.entity.Teacher;
import com.chaopei.znpk.entity.TeacherCourse;
import com.chaopei.znpk.service.CourseService;
import com.chaopei.znpk.service.TeacherCourseService;
import com.chaopei.znpk.tools.Teacher_Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/tc")
public class TeacherCourseController {


    @Autowired
    private TeacherCourseService teacherCourseService;

    //1.下载对应的excel模板
    /**
     * excel文件下载
     * @param response
     * @return
     */
    @GetMapping("/downloadExcel")
    public void downloadExcel(HttpServletResponse response) {
        OutputStream outputStream = null;
        try {
            /*设置返回数据信息*/
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            //防止中文乱码
            String fileName = URLEncoder.encode("测试","UTF-8");
            response.setHeader("Content-Disposition","attachment;filename*=UTF-8''"+fileName+".xlsx");

            outputStream = response.getOutputStream();
            ExcelWriterBuilder write = EasyExcel.write(outputStream, TeacherCourse.class);
            //工作表对象
            ExcelWriterSheetBuilder sheet = write.sheet();

            //准备数据
//            List<Course> list = courseMapper.getAllCourse();
            List<TeacherCourse> list = new ArrayList<>();
            list.add(new TeacherCourse());
            //写
            sheet.doWrite(list);
            outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
//        return new ResponseResult(200,"下载完毕！");
    }

    //2.能够上传对应的文件处理出来数据
    /**
     * excel文件上传，批量添加
     * @param file
     * @return
     */
    private Map<String,Object> map;
    @PostMapping("/uploadExcel")
    public Object uploadExcel(MultipartFile file) {
        List<TeacherCourse> list = new ArrayList<>();

        try {
            EasyExcel.read(file.getInputStream(), TeacherCourse.class,new AnalysisEventListener<TeacherCourse>() {
                /**
                 * 解析每一行数据都会触发
                 * @param teacherCourse
                 * @param analysisContext
                 */
                @Override
                public void invoke(TeacherCourse teacherCourse, AnalysisContext analysisContext) {
//                    boolean add = list.add(teacher);
                    list.add(teacherCourse);
                }

                /**
                 * 解析完所有数据后触发，可以调用持久层方法写入数据库
                 * @param analysisContext
                 */
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    map = teacherCourseService.addList(list);
//                    map =  teacherService.addList(list);
                    System.out.println("excel文件读取完毕！");
                }

            }).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    //3.能够添加单个数据
    @PostMapping("/addOneTeacher")
    public Object addOneTeacher(@RequestBody TeacherCourse teacherCourse) {
        return teacherCourseService.addOneTeacherCourse(teacherCourse);
    }

    //4.能够给出对应的查询的方法（例如教师中提供返回所有教师信息等）1
    @GetMapping("/getTeacherList")
    public Object getTeacherList(Teacher_Course teacherCourse) {
        return teacherCourseService.getTeacherList(teacherCourse);
    }
}
